﻿namespace UnityEditor
{
    using System;
    using System.Collections;
    using UnityEngine;

    [CustomEditor(typeof(Texture3D)), CanEditMultipleObjects]
    internal class Texture3DInspector : TextureInspector
    {
        private Material m_Material;
        private Mesh m_Mesh;
        private PreviewRenderUtility m_PreviewUtility;
        public Vector2 previewDir = new Vector2(0f, 0f);

        public override string GetInfoString()
        {
            Texture3D target = base.target as Texture3D;
            object[] args = new object[] { target.width, target.height, target.depth, TextureUtil.GetTextureFormatString(target.format), EditorUtility.FormatBytes(TextureUtil.GetRuntimeMemorySize(target)) };
            return string.Format("{0}x{1}x{2} {3} {4}", args);
        }

        private void InitPreview()
        {
            if (this.m_PreviewUtility == null)
            {
                this.m_PreviewUtility = new PreviewRenderUtility();
                this.m_PreviewUtility.m_CameraFieldOfView = 30f;
                this.m_Material = new Material("Shader \"Hidden/3DTextureInspector\" {\n                        Properties {\n\t                        _MainTex (\"\", 3D) = \"\" { TexGen ObjectLinear }\n                        }\n                        SubShader {\n                            Tags { \"ForceSupported\" = \"True\" } \n\t                        Pass { SetTexture[_MainTex] { combine texture } }\n                        }\n                        Fallback Off\n                        }");
                this.m_Material.hideFlags = HideFlags.HideAndDontSave;
                this.m_Material.shader.hideFlags = HideFlags.HideAndDontSave;
                this.m_Material.mainTexture = base.target as Texture;
            }
            if (this.m_Mesh == null)
            {
                GameObject obj2 = (GameObject) EditorGUIUtility.LoadRequired("Previews/PreviewMaterials.fbx");
                obj2.SetActive(false);
                IEnumerator enumerator = obj2.transform.GetEnumerator();
                try
                {
                    while (enumerator.MoveNext())
                    {
                        Transform current = (Transform) enumerator.Current;
                        if (current.name == "sphere")
                        {
                            this.m_Mesh = current.GetComponent<MeshFilter>().sharedMesh;
                        }
                    }
                }
                finally
                {
                    IDisposable disposable = enumerator as IDisposable;
                    if (disposable == null)
                    {
                    }
                    disposable.Dispose();
                }
            }
        }

        public void OnDisable()
        {
            if (this.m_PreviewUtility != null)
            {
                this.m_PreviewUtility.Cleanup();
                this.m_PreviewUtility = null;
            }
            if (this.m_Material != null)
            {
                UnityEngine.Object.DestroyImmediate(this.m_Material.shader, true);
                UnityEngine.Object.DestroyImmediate(this.m_Material, true);
                this.m_Material = null;
            }
        }

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
        }

        public override void OnPreviewGUI(Rect r, GUIStyle background)
        {
            if (!ShaderUtil.hardwareSupportsRectRenderTexture || !SystemInfo.supports3DTextures)
            {
                if (Event.current.type == EventType.Repaint)
                {
                    EditorGUI.DropShadowLabel(new Rect(r.x, r.y, r.width, 40f), "3D texture preview not supported");
                }
            }
            else
            {
                this.previewDir = PreviewGUI.Drag2D(this.previewDir, r);
                if (Event.current.type == EventType.Repaint)
                {
                    this.InitPreview();
                    this.m_Material.mainTexture = base.target as Texture;
                    this.m_PreviewUtility.BeginPreview(r, background);
                    bool fog = RenderSettings.fog;
                    Unsupported.SetRenderSettingsUseFogNoDirty(false);
                    this.m_PreviewUtility.m_Camera.transform.position = (Vector3) (-Vector3.forward * 3f);
                    this.m_PreviewUtility.m_Camera.transform.rotation = Quaternion.identity;
                    Quaternion rot = Quaternion.Euler(this.previewDir.y, 0f, 0f) * Quaternion.Euler(0f, this.previewDir.x, 0f);
                    this.m_PreviewUtility.DrawMesh(this.m_Mesh, Vector3.zero, rot, this.m_Material, 0);
                    this.m_PreviewUtility.m_Camera.Render();
                    Unsupported.SetRenderSettingsUseFogNoDirty(fog);
                    Texture image = this.m_PreviewUtility.EndPreview();
                    GUI.DrawTexture(r, image, ScaleMode.StretchToFill, false);
                }
            }
        }

        public override void OnPreviewSettings()
        {
            if (ShaderUtil.hardwareSupportsRectRenderTexture && SystemInfo.supports3DTextures)
            {
                GUI.enabled = true;
            }
        }
    }
}

